<?php

namespace App\Console\Commands\Test;

use App\Helpers\ArrayHelper;
use App\Helpers\ProjectHelper;
use App\Http\Controllers\Constant\Constant;
use App\Http\Controllers\Logic\ApproveHistory;
use App\Http\Controllers\OATrait\OATrait;
use App\Model\OAFinance;
use App\Model\OAPosition;
use App\Model\OAPositionMapping;
use App\Model\OAProjectLx;
use App\Model\OAProjectYs;
use App\Model\OAProjectYsDetail;
use App\Model\OAStaff;
use App\Model\OATest;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\IOFactory;

class lfxTest extends Command
{
    use OATrait;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'lfx:test {--name=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '李飞翔专用测试';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     * @throws Exception
     */
    public function handle()
    {
       $arr1 = [['name']];
    }
    // 模版导出(读写)

    /**
     * @throws Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
    public function templateExport($templateFile)
    {
       // $template_path = __DIR__ . '/PayrollTemplate.xls';
        // 读取模板
        $spreadsheet = IOFactory::load($templateFile);

        // 以下代码行将活动工作表索引设置为第一个工作表：设置Excel Sheet
        $spreadsheet->setActiveSheetIndex(0);

        // 指向激活的工作表
        $worksheet = $spreadsheet->getActiveSheet();

        $worksheet->getCell('A1')->setValue('John');
        $worksheet->getCell('B1')->setValue('Smith');

        // 样式设置 加边框
        $styleThinBlackBorderOutline = array(
            'borders' => [
                'allBorders' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                    'color' => ['argb' => '666666'],
                ],
            ]
        );
        $worksheet->getStyle('A6:H' . 10)->applyFromArray($styleThinBlackBorderOutline);

        // 结束 进行导出
        ob_end_clean(); // 清空（擦除）缓冲区并关闭输出缓冲
        ob_start(); // 打开输出控制缓冲

        $date = date("Ymd", time());
        $listname = 'hello' . $date;

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $listname . '.xlsx"');
        header('Cache-Control: max-age=0');

        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');

}

    protected function formatArr(array $array)
    {
       return array_map(function ($val){
            return strval($val);
        },$array);
    }


    private function fixedLx()
    {

//        $r = OAProjectLx::query()->whereJsonContains('role_list',"215")
//                            ->whereJsonContains('approver',"195")
//                            ->first();
//       dd($r);

        $projects = OAProjectLx::all();
        $projects->each(function (OAProjectLx $lx){
            $roles = explode(',',$lx->role_list);
            $uids = explode(',',$lx->approver);
            $uids[] = strval($lx->uid);
            $mappingRoles = OAPositionMapping::getPositionMapping($roles);
            //$lx->shenpi = ["roles"=>$mappingRoles,'uids'=>$uids];
            $lx->approver = json_encode($this->formatArr($uids));
            $lx->role_list = json_encode($this->formatArr($mappingRoles));
            $lx->save();
        });
    }

    private function fixedYs()
    {
        $ysList = OAProjectYs::all();
        $ysList->each(function (OAProjectYs $ys){
            if(is_array($ys->role_list)){
                $approver = $ys->approver;
                $approver = array_map(function ($val){
                    return strval($val);
                },$approver);
                if($ys->seller_id){
                    $approver[] = strval($ys->seller_id);
                }
                $roles = array_map(function ($item){
                    return strval($item);
                },$ys->role_list);
                $ys->role_list = $roles;
                $ys->approver = $approver;
            }else{
                $roles = explode(',',$ys->role_list);
                $uids = explode(',',$ys->approver);
                $uids[] = $ys->uid;
                if($ys->seller_id){
                    $uids[] = $ys->seller_id;
                }
                $mappingRoles = OAPositionMapping::getPositionMapping($roles);
                $ys->approver = json_encode($this->formatArr($uids));
                $ys->role_list = json_encode($this->formatArr($mappingRoles));
            }
            $ys->deptid = optional($ys->projectLx)->deptid ?? 0;
            $ys->save();
        });
    }

    private function fixedCw()
    {
        $cwList = OAFinance::all();
        $cwList->each(function (OAFinance $cw){

            if(is_array($cw->role_list)){
                $approver = $cw->approver;
                $approver = array_map(function ($val){
                    return strval($val);
                },$approver);
                $roles = array_map(function ($item){
                    return strval($item);
                },$cw->role_list);
                $cw->role_list = $roles;
                $cw->approver = $approver;
            }else {
                $roles = explode(',', $cw->role_list);
                $uids = explode(',', $cw->approver);
                $uids[] = strval($cw->uid);
                $mappingRoles = OAPositionMapping::getPositionMapping($roles);
                //dd(json_encode($uids));
                $cw->approver = json_encode($this->formatArr($uids));
                $cw->role_list = json_encode($this->formatArr($mappingRoles));
            }
            $cw->deptid = optional($cw->lx)->deptid ?? 0;
            $cw->save();
        });

    }

}
